package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.jpay.ext.kit.IpKit;
import com.jpay.ext.kit.StrKit;
import com.qianniu.napi.admin.entity.Consult;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.service.IConsultService;
import com.qianniu.napi.common.util.ObjectUtil;
import nl.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 *
 * <p>
 * 商家咨询
 * </p>
 *
 * @author chenxuebin
 * @since 2018-10-16
 */
@RestController
public class ConsultController {

    private static final Logger logger = LoggerFactory.getLogger(ConsultController.class);

    @Autowired
    private IConsultService consultService;

    @RequestMapping("/api/consult/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = consultService.selectPage(page, new EntityWrapper<Consult>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/consult/add")
    @ResponseBody
    public Jinfo consultAdd(@Valid Consult consult,
                          BindingResult bindingResult,HttpServletRequest req) {
        Jinfo info = new Jinfo();
        consult.setCreatetime(new Date());
        consult.setMid(ObjectUtil.getMid());
        consult.setMpid(ObjectUtil.getMpid());
        consult.setUid(ObjectUtil.getUid());
//        String ip = IpKit.getRealIp(req);
        String ip = ObjectUtil.getIP(req);
        if (StrKit.isBlank(ip)) {
            ip = "127.0.0.1" ;
        }
        consult.setIp(ip);
        String ua = req.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(req.getHeader("User-Agent"));
        String os = userAgent.getOperatingSystem().toString();

//        if (os.equals("android")) {
//            String rex="[()]+";
//            String[] str=ua.split(rex);
//            str = str[1].split("[;]");
//
//            //遍历
//            String[] res = null;
//            for (String str1 : str) {
//                if(str1.contains("Build/")){
//                    res = str1.split("Build/");
//                     =res[0]);
//                }
//            }
//        }else if (os.equals("iphone")) {
//            String[] str=ua.split("[()]+");
//            String res="iphone"+str[1].split("OS")[1].split("like")[0];
//            System.out.println(res);
//        }else if (os.equals("ipad")) {
//            System.out.println("ipad");
//        }else {
//            System.out.println(os);
//        }
        consult.setTerminal(os);
        consult.insert();
        return info;
    }

    @RequestMapping("/api/consult/modify")
    @ResponseBody
    public Jinfo consultUpdate(HttpServletRequest req,
                                    Consult consult) {
        Jinfo info = new Jinfo();
        consult.updateById();
        return info;
    }

    @RequestMapping("/api/consult/detail")
    @ResponseBody
    public Jinfo getConsultDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Consult consult = consultService.selectById(id);
        info.setInfo(consult);
        return info;
    }
}
